package com.snowflake.ecatalog.service.profile;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.snowflake.ecatalog.contant.CompanyLaunchType;
import com.snowflake.ecatalog.contant.ResourceInfoType;
import com.snowflake.ecatalog.contant.Status;
import com.snowflake.ecatalog.entity.CompanyLaunch;
import com.snowflake.ecatalog.entity.ResourceInfo;
import com.snowflake.ecatalog.repository.CompanyLaunchDao;
import com.snowflake.ecatalog.util.LoginUtil;

@Service
@Transactional(readOnly = true)
public class CompanyLaunchService {
	@Autowired
	private CompanyLaunchDao companyLaunchDao;
	@Autowired
	private ResouceInfoService resouceInfoService;
	private static final int RESOURCEINFO_TYPE = ResourceInfoType.LAUNCH;

	public CompanyLaunch getCompanyLaunch(Long id) {
		return companyLaunchDao.findOne(id);
	}

	@Transactional(readOnly = false)
	public void saveCompanyLaunch(CompanyLaunch launch, List<ResourceInfo> resourceInfoList) {
		launch.setCreator(LoginUtil.getCurrentUser());
		launch.setCreatedTime(new Date());
		launch.setStatus(Status.DISABLE);
		CompanyLaunch savedLaunch = companyLaunchDao.save(launch);
		for (ResourceInfo resourceInfo : resourceInfoList) {
			resourceInfo.setOwnerId(savedLaunch.getId());
			resourceInfo.setOwnerType(RESOURCEINFO_TYPE);
		}
		resouceInfoService.saveResourceInfo(resourceInfoList);
	}

	@Transactional(readOnly = false)
	public void updateCompanyLaunch(CompanyLaunch launch, List<ResourceInfo> resourceInfoList) {
		launch.setLastModifier(LoginUtil.getCurrentUser());
		launch.setLastModifiedTime(new Date());
		CompanyLaunch savedLaunch = companyLaunchDao.save(launch);
		// 更新ResourceInfo，保存新资源，删除无效资源
		resouceInfoService.updateResourceInfo(savedLaunch.getId(), RESOURCEINFO_TYPE, resourceInfoList);
	}

	@Transactional(readOnly = false)
	public void deleteCompanyLaunch(Long launchId) {
		resouceInfoService.deleteResourceInfoFile(launchId, RESOURCEINFO_TYPE);
		companyLaunchDao.delete(launchId);
	}

	public List<CompanyLaunch> getAllCompanyLaunch() {
		return (List<CompanyLaunch>) companyLaunchDao.findAll();
	}

	public Map<Integer, ResourceInfo> getImgPathMap(Long launchId) {
		return resouceInfoService.getImgPathMap(launchId, RESOURCEINFO_TYPE);
	}

	public Page<CompanyLaunch> getCompanyLaunchs(Long profileId, Integer launchType, Map<String, Object> filterParams,
			int pageNumber, int pageSize) {
		PageRequest pageRequest = buildPageRequest(pageNumber, pageSize);
		Specification<CompanyLaunch> spec = buildSpecification(profileId, launchType, filterParams);
		return companyLaunchDao.findAll(spec, pageRequest);
	}

	@Transactional(readOnly = false)
	public void enableCompanyLauch(Long id) {
		companyLaunchDao.changeStatus(Status.ENABLE, id);
	}

	@Transactional(readOnly = false)
	public void disableCompanyLauch(Long id) {
		companyLaunchDao.changeStatus(Status.DISABLE, id);
	}
	
	public boolean checkEnableCompanyLauch(Long id,Integer launchType){
		boolean result = true;
		int limit = 10;
		switch (launchType) {
		case CompanyLaunchType.STARTUP:
			limit = 1;	//广告图片：一张图
			break;
		case CompanyLaunchType.BACKGROUND:
			limit = 1;	//背景图片：一张图
			break;
		case CompanyLaunchType.CONTRACT:
			limit = 1;	//联系信息:一张图片
			break;
		case CompanyLaunchType.ENVIRONMENT:
			limit = 3;	//公司图片：三张图片
			break;
		case CompanyLaunchType.INTRODUCTION:
			limit = 4; //公司简介：两张图片+两段视频
			break;
		}
		List<ResourceInfo> resourceInfos = resouceInfoService.findByOwnerIdAndOwnerType(id, ResourceInfoType.LAUNCH);
		
		if(CollectionUtils.isEmpty(resourceInfos) || resourceInfos.size() < limit){
			result = false;
		}
		return result;
	}

	@Transactional(readOnly = false)
	public void enableCompanyLauch(Long profileId, Integer launchType, Long id) {
		companyLaunchDao.disableCompanyLauch(profileId, launchType);
		enableCompanyLauch(id);
	}
	
	public long getEnableCompanyLauchCount(Long profileId,Integer launchType){
		return companyLaunchDao.count(profileId,launchType,Status.ENABLE);
	}

	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize) {
		return new PageRequest(pageNumber - 1, pagzSize);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<CompanyLaunch> buildSpecification(Long profileId, Integer launchType,
			Map<String, Object> filterParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("profile.id", new SearchFilter("profile.id", Operator.EQ, profileId));
		filters.put("type", new SearchFilter("type", Operator.EQ, launchType));
		Specification<CompanyLaunch> spec = DynamicSpecifications.bySearchFilter(filters.values(), CompanyLaunch.class);
		return spec;
	}
	
}
